import vue from "@vitejs/plugin-vue";
import {fileURLToPath, URL} from "node:url";
import process from "node:process";
import {join, resolve} from "node:path";
import {existsSync} from 'node:fs';
import {defineConfig, loadEnv} from "vite";
import {createHtmlPlugin} from "vite-plugin-html";
import tailwindcss from "@tailwindcss/vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";



export default defineConfig(({mode}) => {
    const env = loadEnv(mode, process.cwd(), "");

    return {
        plugins: [
            vue(),
            tailwindcss(),
            createHtmlPlugin({
                inject: {
                    data: {title: env.VITE_APP_TITLE},
                },
            }),
            AutoImport({
                resolvers: [ElementPlusResolver()],
                imports: ["vue",],
                dirs: [resolve(__dirname, "src/stores")],
                dts: "src/auto-imports.d.ts", // 生成类型声明
                vueTemplate: true, // 在模板中也支持
            }),
            Components({
                resolvers: [
                    ElementPlusResolver({
                        importStyle: "sass", // 使用 Sass 风格的样式
                        directives: true, // 自动导入 Element Plus 指令
                    }),
                ],
                directoryAsNamespace: true,
            }),
        ],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src", import.meta.url)),
            },
        },
        server: {
            open: "/login",
            host: "0.0.0.0",
            port: 8080,
            proxy: {
                "/api": {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ""),
                },
            },
        },
    };
});
